package com.gable.report.service.impl;

import com.gable.report.dao.IChartsForHospitalBaseDataDao;
import com.gable.report.dao.IChartsHospitalSearchDao;
import com.gable.report.model.ChartsAssetsBaseDataView;
import com.gable.report.model.ChartsBaseDataView;
import com.gable.report.model.ChartsHospitalQuery;
import com.gable.report.model.ChartsPriceCategory;
import com.gable.report.service.ChartsForHospitalBaseDataService;
import com.gable.report.util.ChartsUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

import static com.gable.report.util.ChartsUtil.getParamMap;


/**
 * Created by lvdouStart on 2017/7/19.
 * 医院报表基本数据service实现类
 */
@Service
public class ChartsForHospitalBaseDataServiceImpl implements ChartsForHospitalBaseDataService {

    private final Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    IChartsForHospitalBaseDataDao chartsForHospitalBaseDataDao;

    @Autowired
    IChartsHospitalSearchDao chartsHospitalSearchDao;

    @Override
    public Map<String, Object> queryAllAssetsForDepartmentIndex(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        Map<String,Object> returnMap = new HashMap<>();
        ChartsBaseDataView allAssets = chartsForHospitalBaseDataDao.queryAllAssets(paramMap);
        List<ChartsBaseDataView> list = chartsForHospitalBaseDataDao.queryDepartments(paramMap);
        if(list.size() < 7){
            returnMap.put("department",list);
        }else {
            //组装科室详情饼图,6个以内
            List<ChartsBaseDataView> returnList = new ArrayList<>();
            Long price = 0L;
            Long count = 0L;
            for(int i=0;i<5;i++){
                price += list.get(i).getCurPrice();
                count += list.get(i).getCurCounts();
                returnList.add(list.get(i));
            }
            ChartsBaseDataView obj = new ChartsBaseDataView();
            obj.setName("其他");
            obj.setCurPrice(allAssets.getCurPrice() - price);
            obj.setCurCounts(allAssets.getCurCounts()-count);
            returnList.add(obj);
            returnMap.put("department",returnList);
        }
        List<ChartsBaseDataView> deviceFirstTypeList = chartsForHospitalBaseDataDao.queryFirstDeviceTypes(paramMap);

        returnMap.put("allAssets",allAssets);
        returnMap.put("firstTypeList",deviceFirstTypeList);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryAllAssetsForDeviceIndex(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String,Object> returnMap = new HashMap<>();
        Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        ChartsBaseDataView allAssets = chartsForHospitalBaseDataDao.queryAllAssets(paramMap);
        ChartsPriceCategory chartsPriceCategory = chartsHospitalSearchDao.queryPriceCategory(String.valueOf(hospitalId));
        if(chartsPriceCategory == null){
            chartsPriceCategory = chartsHospitalSearchDao.queryPriceCategory("1001");
        }
        Map<String,Object> priceCategoryNameMap = ChartsUtil.getDevicePriceCategoryName(chartsPriceCategory);
        //组装金额区间
        paramMap.put("asName",priceCategoryNameMap.get("asName"));
        List<ChartsBaseDataView> list = chartsForHospitalBaseDataDao.queryDevicePriceCategory(paramMap);
        List<ChartsBaseDataView> priceCategoryList = new ArrayList();//返回的金额区间情况
        //查询结果可能发生缺少的情况 通过下面方法补全
        List<String> priceCategoryNameList = (List<String>)priceCategoryNameMap.get("priceCategory");
        for(String str : priceCategoryNameList){
            ChartsBaseDataView cbv = null;
            for(ChartsBaseDataView obj : list){
                if(obj != null && obj.getName() != null && str.equals(obj.getName())){
                    cbv = obj;
                }
            }
            if(cbv == null){
                cbv = new ChartsBaseDataView();
                cbv.setName(str);
            }
            priceCategoryList.add(cbv);
        }
        returnMap.put("allAssets",allAssets);
        returnMap.put("amountList",priceCategoryList);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryAllAssetsForDeviceAgeLimit(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String,Object> returnMap = new HashMap<>();
        Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        ChartsBaseDataView allAssets = chartsForHospitalBaseDataDao.queryAllAssets(paramMap);
        Map<String,Object>  ageLimitCategoryMap = ChartsUtil.getAgeLimitCategoryName(null); //返回的年限指数区间情况
        //组装年限指数区间
        paramMap.put("asNameCur",ageLimitCategoryMap.get("asNameCur"));
        paramMap.put("asNameLast",ageLimitCategoryMap.get("asNameLast"));
        List<ChartsBaseDataView> list = chartsForHospitalBaseDataDao.queryDeviceAgeLimitCategory(paramMap);
        List<ChartsBaseDataView> ageLimitCategoryList = new ArrayList();//返回的年限指数区间情况
        //查询结果可能发生缺少的情况 通过下面方法补全
        List<String> ageLimitCategoryNameList = (List<String>)ageLimitCategoryMap.get("ageLimitCategory");
        for(String str : ageLimitCategoryNameList){
            ChartsBaseDataView cbv = null;
            for(ChartsBaseDataView chartsBaseDataView : list){
                if(chartsBaseDataView != null && chartsBaseDataView.getName() != null && str.equals(chartsBaseDataView.getName())){
                    cbv = chartsBaseDataView;
                }
            }
            if(cbv == null){
                cbv = new ChartsBaseDataView();
                cbv.setName(str);
            }
            ageLimitCategoryList.add(cbv);
        }
        returnMap.put("allAssets",allAssets);
        returnMap.put("ageLimitList",ageLimitCategoryList);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryDepartments(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String,Object> returnMap = new HashMap<>();
        Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        ChartsBaseDataView allAssets = chartsForHospitalBaseDataDao.queryAllAssets(paramMap);
        List<ChartsBaseDataView> list = chartsForHospitalBaseDataDao.queryDepartments(paramMap);
        //设置平均指方便气泡图四个象限设置
        Double avgPrice = 0.0;
        Double avgCounts = 0.00;
        if(allAssets != null && list != null && list.size() != 0){
            avgPrice = allAssets.getCurPrice()*1.0/list.size();
            avgCounts = allAssets.getCurCounts()*1.0/list.size();
        }
        returnMap.put("avgPrice",avgPrice);
        returnMap.put("avgCounts",avgCounts);
        returnMap.put("avgAgeLimitMeasure",allAssets.getCurAgeLimitMeasure());
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String,Object> queryDeviceTypes (Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String,Object> returnMap = new HashMap<>();
        Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        ChartsBaseDataView allAssets = chartsForHospitalBaseDataDao.queryAllAssets(paramMap);
        List<ChartsBaseDataView> list = chartsForHospitalBaseDataDao.querySecondDeviceTypes(paramMap);
        //设置平均指方便气泡图四个象限设置
        Double avgPrice = 0.0;
        Double avgCounts = 0.00;
        if(allAssets != null && list != null && list.size() != 0){
            int size = 0; //二级分类数量
            for(ChartsBaseDataView superObj : list){
                if(!CollectionUtils.isEmpty(superObj.getList()))
                    size = size + superObj.getList().size(); //计算二级设备分类种数
            }
            avgPrice = allAssets.getCurPrice()*1.0/size;
            avgCounts = allAssets.getCurCounts()*1.0/size;
        }
        returnMap.put("avgPrice",avgPrice);
        returnMap.put("avgCounts",avgCounts);
        returnMap.put("avgAgeLimitMeasure",allAssets.getCurAgeLimitMeasure());
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryDeviceModels(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String,Object> returnMap = new HashMap<>();
        Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        ChartsBaseDataView allAssets = chartsForHospitalBaseDataDao.queryAllAssets(paramMap);
        List<ChartsBaseDataView> list = chartsForHospitalBaseDataDao.queryDeviceModels(paramMap);
        //设置平均指方便气泡图四个象限设置
        Double avgPrice = 0.0;
        Double avgCounts = 0.00;
        if(allAssets != null && list != null && list.size() != 0){
            avgPrice = allAssets.getCurPrice()*1.0/list.size();
            avgCounts = allAssets.getCurCounts()*1.0/list.size();
        }
        returnMap.put("avgPrice",avgPrice);
        returnMap.put("avgCounts",avgCounts);
        returnMap.put("avgAgeLimitMeasure",allAssets.getCurAgeLimitMeasure());
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public  Map<String,Object> queryDeviceBrands(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String,Object> returnMap = new HashMap<>();
        Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        List<ChartsBaseDataView> list = chartsForHospitalBaseDataDao.queryDeviceBrands(paramMap);
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public  Map<String,Object> queryDeviceInfoByModelName(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String,Object> returnMap = new HashMap<>();
        Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        List<ChartsAssetsBaseDataView> list = chartsForHospitalBaseDataDao.queryDeviceInfoByModelName(paramMap);
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public List<ChartsBaseDataView> queryYearTrend(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        Calendar calendar = Calendar.getInstance();
        Integer year = calendar.get(Calendar.YEAR);
        //拼接当前年份前三年的id,名称
        for(int i=2;i>=0;i--){
            paramMap.put("asId"+i,year-i-1);
            paramMap.put("asName"+i,year-i-1);
            paramMap.put("year"+i,(year-i)+"-1-1");
        }
        //拼接当前年份的id,name
        paramMap.put("asId",year);
        paramMap.put("asName",year);
        List<ChartsBaseDataView> list = chartsForHospitalBaseDataDao.queryYearTrend(paramMap);
        return list;
    }

    @Override
    public Map<String, Object> queryNewlyOrScrapAssetsInfo(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String,Object> returnMap = new HashMap<>();
        Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        List<ChartsAssetsBaseDataView> list = chartsForHospitalBaseDataDao.queryNewlyOrScrapAssetsInfo(paramMap);
        returnMap.put("list",list);
        return returnMap;
    }

}
